﻿#include <iostream>
#include<curl/curl.h>
#include<map>
#include<regex>
using namespace std;
const static string URL = "http://ssr1.scrape.center";

size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
	size_t totalSize = size * nmemb;
	output->append((char*)contents, totalSize);
	return totalSize;
}

string request_get(const string& url, map<string, string> headers = map<string, string>()) {
	CURL* curl;
	CURLcode statusCode;
	string htmlData;
	curl_global_init(CURL_GLOBAL_DEFAULT);
	curl = curl_easy_init();
	if (curl) {
		curl_easy_setpot(curl, CURLOPT_SSL_VERIFYPEER, 0);
		if (headers.find("cookies") != headers.end()) {
			curl_easy_setopt(curl, CURLOPT_COOKIE, heders["cookies"].c_str());
		}
		curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlData);
		auto res = curl_easy_perform(curl);
		if (res != CURLE_OK) {
			std::cerr << "curl_easy_perform()failed:" << curl_easy_strerror(res) << std::endl;
		}
		curl_easy_cleanup(curl);
		return htmlData;
	}
	else {
		std::cerr << "curl_easy_init()failed." << std::endl;
		return"";
	}
	curl_global_cleanup();
}

void getURL() {
	string url;
	string html;
	regex reg("/detail/\\d+");
	smatch result;
	for (int page = 1; page <= 2; ++page) {
		url = URL + "/page/" + to_string(page);
		string html = request_get(url);
		auto end_it = sregex_iterator();
		sregex_iterator it(html.begin(), html.end(), reg);
		for (; it != end_it, ++it) {
			result = *it;
			cout << result.str() << endl;
		}
	}
}

void parseDetailpage(const string& url) {
	string html = request_get(url);
	smatch ResultTitle, ResultCategories, ResultArea, ResultTime, ResultPublish;
	regex RegTitle(">(.*</h2>");
	regex_search(html, ResultTitle, RegTitle);
	cout << ResultTitle[1].str() << endl;
	regex RegCategories("<span>(.*)</span>");
	vector<string> vt = findAll(html, RegCategories, 1);
	vt.pop_back();
	regex RegArea("\>([^x00-xff^/]+)</span>");
	regex RegTime(">(\\d+).*</span>");
	regex_search(html, ResultTime, RegTime);
	cout << ResultTime[1].str() << endl;
	regex RegPublish("\\d+-\\d+-\\d+");
	regex_search(html, ResultPublish, RegPublish);
	cout << ResultPublish.str() << endl;
}
int main()
{
	string html = request_get(URL);
	cout << html << endl;
	getURL();
}